package com.wnm.web;

import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wnm.pojo.User;
import com.wnm.service.UserService;
import com.wnm.service.impl.UserServiceImpl;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
//	校验用户名是否存在
	public void isExist(HttpServletRequest request, HttpServletResponse response) {
		UserService userService = new UserServiceImpl();
		boolean flag = userService.isExist(request.getParameter("username"));
		ObjectMapper om = new ObjectMapper();
		try {
			om.writeValue(response.getWriter(), !flag);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

//注册
	public void regist(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, String[]> map = request.getParameterMap();
		User user = new User();
		BeanUtils.populate(user, map);
		UserService userService = new UserServiceImpl();
		userService.regist(user);
		response.sendRedirect(request.getContextPath() + "/pages/registSuccess.jsp");
	}

//更改密码
	public void updatePassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String newPassword = request.getParameter("newPassword");
		String oldPassword = request.getParameter("oldPassword");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		UserService userService = new UserServiceImpl();
		userService.updatePasswordByUid(user.getId(), newPassword);
	}

//上传头像
	public void upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		User userSession = (User) session.getAttribute("user");

	}

//激活
	public void active(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter("code");
		UserService userService = new UserServiceImpl();
		boolean flag = userService.active(code);
		if (flag) {
			response.sendRedirect(request.getContextPath() + "/pages/activeSuccess.jsp");
		} else {
			response.sendRedirect(request.getContextPath() + "/pages/activeError.jsp");

		}
	}

//登录
	public void login(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UserService userService = new UserServiceImpl();
		User user = userService.login(username, password);
		if (user == null) {
			request.setAttribute("msguser", "用户名或密码错误");
			request.getRequestDispatcher("/pages/login.jsp").forward(request, response);
		} else {
			String status = user.getStatus();
			if ("1".equals(status)) {
				request.getSession().setAttribute("user", user);
				response.sendRedirect(request.getContextPath() + "/pages/index.jsp");
			} else {
				request.setAttribute("msguser", "账号未激活，请使用邮箱激活");
				request.getRequestDispatcher("/pages/login.jsp").forward(request, response);
			}

		}
	}

//验证码
	public void checkCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String checkcode = request.getParameter("checkcode");
		String _checkcode = (String) request.getSession().getAttribute("checkCode");
		ObjectMapper om = new ObjectMapper();
		System.out.println(_checkcode);
		System.out.println(checkcode);
		if (_checkcode.equalsIgnoreCase(checkcode)) {
			om.writeValue(response.getWriter(), true);
		} else {
			om.writeValue(response.getWriter(), false);
		}
	}

//退出
	public void exist(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.getSession().removeAttribute("user");
		request.getRequestDispatcher("/pages/index.jsp").forward(request, response);

	}
}
